﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using SelfMadeMillionaire.Core;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Repositories;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Core.Specifications;

namespace SelfMadeMillionaire.BLL.Services
{
	public class UserSessionService : IUserSessionService
	{
		private readonly IUserSessionRepository Repository;

        public UserSessionService(IUserSessionRepository repository)
		{
            Repository = repository;
		}

        public UserSession GetByToken(string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return null;
            }
            return Repository.Get(x => x.SessionToken == token);
        }

        public UserSession GetLatestByUserId(int id)
        {
            return Repository.GetAll(x => x.Subscriber.ID == id).OrderByDescending(x => x.LastActivityDateTime).FirstOrDefault();
        }
        
        public void Insert(UserSession session)
        {
            Repository.Add(session);
        }

        public void Update(UserSession user)
        {
            Repository.Update(user);
        }

        public int Count()
        {
            return Repository.GetAll().Count();
        }
        
	}
}